package com.kscrm.quartz.task;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.kscrm.common.utils.DateUtils;
import com.kscrm.common.utils.StringUtils;
import com.kscrm.web.criteria.ChannelCountCriteria;
import com.kscrm.web.criteria.ClientCountCriteria;
import com.kscrm.web.criteria.ProjectCountCriteria;
import com.kscrm.web.domain.Channel;
import com.kscrm.web.domain.Client;
import com.kscrm.web.domain.Stat;
import com.kscrm.web.enums.ChannelEnum;
import com.kscrm.web.enums.ClientEnum;
import com.kscrm.web.enums.ProjectEnum;
import com.kscrm.web.service.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试 - 客户数据统计
 * 每天凌晨一点 统计前一天的客户数据
 *
 * @author Dhq
 */
@Component("ClientTask")
public class ClientTask {
    @Resource
    private IClientService clientService;

    @Resource
    private IChannelService channelService;

    @Resource
    private IProjectService projectService;

    @Resource
    private IProjectCollectionService projectCollectionService;

    @Resource
    private IStatService statService;


    // 每天凌晨一点 统计前一天的客户数据
    public void init() {
        // 获取昨天的日期
        Date yesterdayDate = DateUtil.beginOfDay(new Date());
        // 本月的日期
        String month = DateUtil.format(yesterdayDate, DatePattern.NORM_MONTH_FORMAT);
        // 前一天的日期
//        String day = DateUtil.format(yesterdayDate, DatePattern.NORM_DATE_PATTERN);

        // 获取渠道列表
        List<Channel> channelList = channelService.selectChannelList(new Channel().setStatus(1));
        // 统计每个渠道的客户数据
        for (Channel channel : channelList) {
            // 获取客户数据（不同类型客户）
//            initClient(channel.getId(), day);
            initClient(channel.getUserId(), month);
            // 获取项目数据（不同类型项目）
//            initProject(channel.getId(), day);
            initProject(channel.getUserId(), month);
            // 获取渠道数据
//            initChannel(channel.getId(), day);
            initChannel(channel.getId(), month);
        }
    }

    // 今日数据 （每小时执行一次 查找前一个小时的）
    public void todayInit() {
        // 查今天的日期
        String day = DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN);
        List<Channel> channelList = channelService.selectChannelList(new Channel().setStatus(1));
        for (Channel channel : channelList) {
            // 获取客户数据（不同类型客户）
            initClient(channel.getId(), day);
            // 获取项目数据（不同类型项目）
            initProject(channel.getId(), day);
            // 获取项目业绩数据
            initProjectMoney(channel.getId(), day);
            // 获取渠道数据
            initChannel(channel.getId(), day);
        }
    }

    // 每天执行一次 凌晨执行 (渠道全部数据)
    public void initChannelData() {
        // 获取渠道列表
        List<Channel> channelList = channelService.selectChannelList(new Channel().setStatus(1));
        // 统计每个渠道的数据
        for (Channel channel : channelList) {
            // 获取客户数据
            channel.setClientNum(clientService.selectClientCount(new ClientCountCriteria().setUserId(channel.getUserId())));
            // 获取项目数据
            channel.setProjectNum(projectService.selectProjectCount(new ProjectCountCriteria().setChannelId(channel.getId())));
            // 获取项目业绩数据
            channel.setMoneyNum(projectCollectionService.sumProjectMoney(new ProjectCountCriteria().setChannelId(channel.getId())));
            // 更新时间
            channel.setUpdateNumTime(new Date());
            channelService.updateChannelNumber(channel);
        }
    }


    // 客户数据统计
    private void initClient(Long channelId, String date) {
        for (ClientEnum type : ClientEnum.values()) {
            int count = clientService.selectClientCount(new ClientCountCriteria()
                    .setChannelId(channelId)
                    .setDate(date)
                    .setType(type.name()));
            statService.insertStat(new Stat()
                    .setChannelId(channelId)
                    .setDate(date)
                    .setCode(type.name())
                    .setQuantity(count));
        }
    }

    // 项目数据统计
    private void initProject(Long channelId, String date) {
        for (ProjectEnum type : ProjectEnum.values()) {
            int count = projectService.selectProjectCount(new ProjectCountCriteria()
                    .setChannelId(channelId)
                    .setDate(date)
                    .setType(type.name()));
            statService.insertStat(new Stat()
                    .setDate(date)
                    .setChannelId(channelId)
                    .setCode(type.name())
                    .setQuantity(count));
        }
    }


    // 项目业绩数据统计
    private void initProjectMoney(Long channelId, String date) {
        for (ProjectEnum type : ProjectEnum.values()) {
            int count = projectCollectionService.sumProjectMoney(new ProjectCountCriteria()
                    .setChannelId(channelId)
                    .setDate(date)
                    .setType(type.name()));
            statService.insertStat(new Stat()
                    .setDate(date)
                    .setChannelId(channelId)
                    .setCode(type.name() + "业绩")
                    .setQuantity(count));
        }
    }

    // 渠道数据统计
    private void initChannel(Long channelId, String date) {

        for (ChannelEnum type : ChannelEnum.values()) {
            int count = 0;

            if (type.name().equals("下级渠道")) {
                count = channelService.selectChannelCount(new ChannelCountCriteria()
                        .setParentId(channelId)
                        .setDate(date));
            }
            if (type.name().equals("客户总数")) {
                count = clientService.selectClientCount(new ClientCountCriteria()
                        .setParentId(channelId)
                        .setDate(date));

            }
            if (type.name().equals("项目总数")) {
                count = projectService.selectProjectCount(new ProjectCountCriteria()
                        .setParentId(channelId)
                        .setDate(date));
            }
            if (type.name().equals("项目业绩")) {
                count = projectCollectionService.sumProjectMoney(new ProjectCountCriteria()
                        .setParentId(channelId)
                        .setDate(date));
            }

            statService.insertStat(new Stat()
                    .setChannelId(channelId)
                    .setDate(date)
                    .setCode(type.name())
                    .setQuantity(count));
        }
    }
}
